package com.itboye.lottery.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.lottery.R;
import com.itboye.lottery.activity.CodeGenerrateActivity;
import com.itboye.lottery.activity.WebActivity;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.base.BaseDialog;
import com.itboye.lottery.bean.OrderBean;
import com.itboye.lottery.bean.PaySupportBean;
import com.itboye.lottery.bean.UserDataBean;
import com.itboye.lottery.callback.PayCallBack;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PayDialog extends BaseDialog implements ResponseCallback {
    private double amount;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_money_value)
    TextView tv_money_value;

    UserPresenter userPresenter;
    @BindView(R.id.rl_yue)
    RelativeLayout rl_yue;

    @BindView(R.id.rl_wx)
    RelativeLayout rl_wx;

    @BindView(R.id.rl_zfb)
    RelativeLayout rl_zfb;

    @BindView(R.id.tv_avaliable_balance)
    TextView tv_avaliable_balance;
    private OrderBean orderBean;

    @BindView(R.id.cb_yue)
    CheckBox cb_yue;
    @BindView(R.id.cb_wx)
    CheckBox cb_wx;
    @BindView(R.id.cb_zfb)
    CheckBox cb_zfb;
    private PayCallBack payCallBack;
    private String payType = "";
    private boolean chongzhi;
    Activity activity;
    //    String return_url = "lottery://com.itboye";
    String return_url = "";

    public PayDialog(Context context) {
        super(context);
    }

    private void initView() {
        setContentView(R.layout.dialog_pay);
        userPresenter = new UserPresenter(this);
    }

    public PayDialog(Context context, boolean fromButton) {
        super(context, fromButton);
        initView();
        try {
            activity = (Activity) context;
        } catch (Exception e) {

        }
    }

    public void setOrderBean(OrderBean orderBean) {
        this.orderBean = orderBean;
        if (chongzhi) {
            setAmount(Double.valueOf(orderBean.getMoney()));
        } else {
            setAmount(orderBean.getAmount());
        }
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    @OnClick(R.id.btn_pay_close)
    void close() {
        dismiss();
    }

    @OnClick(R.id.btn_pay_sure)
    void sure() {
        if (cb_yue.isChecked()) {
            showDialog("正在发起支付，请稍后", activity);
            userPresenter.pay(orderBean.getSn(), amount);
        } else if (cb_wx.isChecked()) {
            showDialog("正在发起支付，请稍后", activity);
            payType = cb_wx.getTag().toString();
            if (chongzhi) {
                userPresenter.chongzhiByFs("", amount, "app", payType, return_url);
            } else {
                userPresenter.zfbH5Pay(orderBean.getSn(), "0", payType, return_url);
            }
        } else if (cb_zfb.isChecked()) {
            showDialog("正在发起支付，请稍后", activity);
            payType = cb_zfb.getTag().toString();
            if (chongzhi) {
                userPresenter.chongzhiByFs("", amount, "app", payType, return_url);
            } else {
                userPresenter.zfbH5Pay(orderBean.getSn(), "0", payType, return_url);
            }
        } else {
            ByAlert.alert("请选择支付方式");
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        userPresenter.paySupport();//获取可用支付方式
        userPresenter.getMyLotteryFragmentData();
//        userPresenter.getWithdrawalBalance();
        tv_money_value.setText(("￥" + amount));
        cb_yue.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                setOtherSelect(0);
            }
        });
        cb_wx.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                setOtherSelect(1);
            }
        });
        cb_zfb.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                setOtherSelect(2);
            }
        });
    }

    private void setOtherSelect(int position) {
        switch (position) {
            case 0:
                cb_zfb.setChecked(false);
                cb_wx.setChecked(false);
                break;
            case 1:
                cb_zfb.setChecked(false);
                cb_yue.setChecked(false);
                break;
            case 2:
                cb_yue.setChecked(false);
                cb_wx.setChecked(false);
                break;
        }
    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        dissDialog();
        if (requestSuccessType == userPresenter.paySupport_SUCCESS) {
            if (!chongzhi) {//非充值界面 余额支付可用
                rl_yue.setVisibility(View.VISIBLE);
            }
            List<PaySupportBean> paySupportBeanList = (List<PaySupportBean>) o;
            for (PaySupportBean paySupportBean : paySupportBeanList) {
//                if (paySupportBean.getPay_type().equals("")) {
//                    rl_yue.setVisibility(View.VISIBLE);
//                    cb_yue.setTag(paySupportBean.getPay_type());
//                } else {
//                    rl_yue.setVisibility(View.GONE);
//                }
                if (paySupportBean.getPay_type().equals("FSB0105") && paySupportBean.getCan_use() == 1) {//微信支付可用
                    rl_wx.setVisibility(View.VISIBLE);
                    cb_wx.setTag(paySupportBean.getPay_type());
                }

                if ((paySupportBean.getPay_type().equals("FSB0115") || paySupportBean.getPay_type().equals("GR_ALIPAY_01")) && paySupportBean.getCan_use() == 1) {//支付宝可用
                    payType = paySupportBean.getPay_type();
                    rl_zfb.setVisibility(View.VISIBLE);
                    cb_zfb.setTag(paySupportBean.getPay_type());
                }
            }

        }
        if (requestSuccessType == userPresenter.getMyLotteryFragmentData_SUCCESS) {
            UserDataBean balanceBean = (UserDataBean) o;
            tv_avaliable_balance.setText("(可用余额" + balanceBean.getKeyong() + ")");
        }
        if (requestSuccessType == userPresenter.pay_SUCCESS) {
            ByAlert.alert("支付成功");
            if (payCallBack != null) {
                payCallBack.paySuccess();
            }
            dismiss();
        }
        if (requestSuccessType == userPresenter.zfbH5Pay_SUCCESS) {
//            Intent intent = new Intent();
//            intent.setAction("android.intent.action.VIEW");
//            Uri content_url = Uri.parse(o + "");
//            intent.setData(content_url);
//            Log.e("weakUpPay",content_url.toString());
            //intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
//            activity.startActivity(intent);
            if (payType.equals("FSB0105")) {//微信支付
                activity.startActivity(new Intent(activity, CodeGenerrateActivity.class).putExtra("url", o + ""));
            } else {
                activity.startActivity(new Intent(activity, WebActivity.class).putExtra("url", o + "").putExtra("title", "支付"));
            }
        }
        if (requestSuccessType == userPresenter.chongzhiByFs_SUCCESS) {
            activity.finish();
            if (payType.equals("FSB0105")) {//微信支付
                activity.startActivity(new Intent(activity, CodeGenerrateActivity.class).putExtra("url", o + ""));
            } else {
                activity.startActivity(new Intent(activity, WebActivity.class).putExtra("url", o + "").putExtra("title", "支付"));
            }
        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        dissDialog();
        if (requestErrorType == userPresenter.paySupport_FAIL) {
            ByAlert.alert(msg);
        }
        if (requestErrorType == userPresenter.getWithdrawalBalance_FAIL) {
            ByAlert.alert(msg);
        }
        if (requestErrorType == userPresenter.pay_FAIL) {
            ByAlert.alert(msg);
            if (payCallBack != null) {
                payCallBack.payFail(msg + "");
            }
        }
        if (requestErrorType == userPresenter.zfbH5Pay_FAIL) {
            ByAlert.alert(msg);
        }
        if (requestErrorType == userPresenter.chongzhiByFs_SUCCESS) {
            ByAlert.alert(msg);
        }

    }

    public void setCallBack(@NotNull PayCallBack payCallBack) {
        this.payCallBack = payCallBack;
    }

    public void setChongzhi(boolean b) {
        this.chongzhi = b;
    }
}
